﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class CreateBooking : Form
    {
        public CreateBooking()
        {
            InitializeComponent();

            DBManager.DisplayDeluxeNonSmokingRoomAvailability(listViewRoomAvailabilityDeluxeNonSmokingRoom);
            DBManager.DisplayDeluxeSmokingRoomAvailability(listViewRoomAvailabilityDeluxeSmokingRoom);
            DBManager.DisplaySuperiorNonSmokingRoomAvailability(listViewRoomAvailabilitySuperiorNonSmokingRoom);
            DBManager.DisplaySuperiorSmokingRoomAvailability(listViewRoomAvailabilitySuperiorSmokingRoom);
        }
            

        private const string BOOKINGSTATUS = "Pending";
        DateTime checkInDate = DateTime.MinValue;
        DateTime checkOutDate = DateTime.MinValue;

        //Ensure that they only put the correct numbers/characters for the selected textbox
        #region
        private void OnlyCharacters_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
        }

        private void onlyNumbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }
        #endregion
        
        private void btnCreateBookingSearchByCustomerNRIC_Click(object sender, EventArgs e)
        {
            tbxCreateBookingSearchCustomerFirstName.Clear();
            DBManager.ListViewManageCustomerInformationByNRIC(listViewCreateBookingCustomerInformation, tbxCreateBookingSearchCustomerNRIC.Text);
        }

        private void btnCreateBookingSearchByCustomerFirstName_Click(object sender, EventArgs e)
        {
            tbxCreateBookingSearchCustomerNRIC.Clear();
            DBManager.ListViewManageCustomerCustomerInformationByName(listViewCreateBookingCustomerInformation, tbxCreateBookingSearchCustomerFirstName.Text);
        }

        private void btnCreateBookingDisplayAllCustomers_Click(object sender, EventArgs e)
        {
            tbxCreateBookingSearchCustomerFirstName.Clear();
            tbxCreateBookingSearchCustomerNRIC.Clear();
            DBManager.ListViewManageCustomerDisplayAllCustomerInformation(listViewCreateBookingCustomerInformation);
        }

        private void listViewCreateBookingCustomerInformation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCreateBookingCustomerInformation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem customerInformation in listViewCreateBookingCustomerInformation.SelectedItems)
                {
                    tbxCreateBookingCustomerNRIC.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].Text;
                    tbxCreateBookingCustomerFirstName.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[1].Text;
                    tbxCreateBookingCustomerLastName.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[2].Text;
                    tbxCreateBookingCustomerOccupation.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[3].Text;
                    tbxCreateBookingCustomerPlaceOfEmployment.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[4].Text;
                    tbxCreateBookingCustomerNationality.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[5].Text;
                    tbxCreateBookingCustomerContactNumber.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[6].Text;
                    tbxCreateBookingCustomerEmail.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[7].Text;
                    tbxCreateBookingCustomerAddress.Text = listViewCreateBookingCustomerInformation.SelectedItems[0].SubItems[8].Text;
                    tbxCreateBookingBookingStatus.Text = BOOKINGSTATUS;
                }
            }
        }


        //List Room Availability
        private void btnRoomAvailabilityRefresh_Click(object sender, EventArgs e)
        {
            DBManager.DisplayDeluxeNonSmokingRoomAvailability(listViewRoomAvailabilityDeluxeNonSmokingRoom);
            DBManager.DisplayDeluxeSmokingRoomAvailability(listViewRoomAvailabilityDeluxeSmokingRoom);
            DBManager.DisplaySuperiorNonSmokingRoomAvailability(listViewRoomAvailabilitySuperiorNonSmokingRoom);
            DBManager.DisplaySuperiorSmokingRoomAvailability(listViewRoomAvailabilitySuperiorSmokingRoom);
        }


        private void btnCreateBookingConfirmCreate_Click(object sender, EventArgs e)
        {
            bool successfulCreate = false;
            
            if (string.IsNullOrEmpty(tbxCreateBookingCustomerNRIC.Text))
            {
                MessageBox.Show("Please Select a Customer that wants to book!");
            }
            else if (string.IsNullOrEmpty(cbxCreateBookingRoomType.Text))
            {
                MessageBox.Show("Please Select a Room Type!");
            }
            else if (string.IsNullOrEmpty(tbxCreateBookingCheckInTime.Text))
            {
                MessageBox.Show("Please Enter a Check In Time!");
            }
            else if (tbxCreateBookingCheckInTime.TextLength < 4)
            {
                MessageBox.Show("Please Enter a Check In Time in 24 hour format from 0000  to 2359. E.g. If 1am enter 0100");
            }
            else if (Convert.ToInt32(tbxCreateBookingCheckInTime.Text) > 2359 || Convert.ToInt32(tbxCreateBookingCheckInTime.Text) < 0000)
            {
                MessageBox.Show("Please Enter a Check In Time in 24 hour format from 0000  to 2359.");
            }
            else if (string.IsNullOrEmpty(tbxCreateBookingCheckOutTime.Text))
            {
                MessageBox.Show("Please Enter a Check Out Time!");
            }
            else if (tbxCreateBookingCheckOutTime.TextLength < 4)
            {
                MessageBox.Show("Please Enter a Check Out Time in 24 hour format) from 1100  to 1200 (11am to 12pm)");
            }
            else if (Convert.ToInt32(tbxCreateBookingCheckOutTime.Text) > 1200 || Convert.ToInt32(tbxCreateBookingCheckOutTime.Text) < 1100)
            {
                MessageBox.Show("Please Enter a Check Out Time in 24 hour format) from 1100  to 1200 (11am to 12pm)");
            }
            else if (string.IsNullOrEmpty(tbxCreateBookingNumberOfAdults.Text))
            {
                MessageBox.Show("Please Enter the number of adults staying in the room. If 0 put 0!");
            }
            else if (string.IsNullOrEmpty(tbxCreateBookingNumberOfAdults.Text))
            {
                MessageBox.Show("Please Enter the number of adult(s) staying in the room. If 0 put 0!");
            }
            else if (string.IsNullOrEmpty(tbxCreateBookingNumberOfChildrens.Text))
            {
                MessageBox.Show("Please Enter the number of children(s) staying in the room. If 0 put 0!");
            }
            else if(string.IsNullOrEmpty(cbxCreateBookingLateCheckOut.Text))
            {
                MessageBox.Show("Please Indicate Whether there is a late checkout!");
            }
            else//Everything filled up
            {
                #region Check Whether the days between the selected check in date and selected check out date is there any days with 0 rooms available
                bool checkForAvailableRoom = true;
                checkInDate = Convert.ToDateTime(dtpCreateBookingCheckInDate.Text);
                checkOutDate = Convert.ToDateTime(dtpCreateBookingCheckOutDate.Text);
                TimeSpan totalBookedDays = checkOutDate - checkInDate;
                int iTotalBookedDays = Convert.ToInt32(totalBookedDays.TotalDays);
                for (int i = 0; i < iTotalBookedDays; i++ )
                {
                    int numberOfRoomsAvailable = DBManager.CheckRoomAvailabilityForCreateBookingAndUpdateBooking(checkInDate.AddDays(i), cbxCreateBookingRoomType.Text);
                    if (numberOfRoomsAvailable == 0)
                    {
                        checkForAvailableRoom = false;
                    }
                }
                #endregion 
                if (checkForAvailableRoom == true)
                {
                    Booking b = new Booking();
                    b.Nric = tbxCreateBookingCustomerNRIC.Text;
                    b.RoomType = cbxCreateBookingRoomType.Text;
                    checkInDate = Convert.ToDateTime(dtpCreateBookingCheckInDate.Text);
                    b.CheckInDate = checkInDate;
                    checkOutDate = Convert.ToDateTime(dtpCreateBookingCheckOutDate.Text);
                    b.CheckOutDate = checkOutDate;
                    b.CheckInTime = tbxCreateBookingCheckInTime.Text;
                    b.CheckOutTime = tbxCreateBookingCheckOutTime.Text;
                    b.NumberOfAdults = Convert.ToInt32(tbxCreateBookingNumberOfAdults.Text);
                    b.NumberOfChildrens = Convert.ToInt32(tbxCreateBookingNumberOfChildrens.Text);
                    b.LateCheckOut = cbxCreateBookingLateCheckOut.Text;
                    if (string.IsNullOrEmpty(tbxCreateBookingRemarks.Text))
                    {
                        b.AdditionalRemarks = null;
                    }
                    else
                    {
                        b.AdditionalRemarks = tbxCreateBookingRemarks.Text;
                    }
                    b.BookingStatus = BOOKINGSTATUS;
                    DBManager.CreateBooking(b);
                    successfulCreate = true;
                }
                else
                {
                    MessageBox.Show("Sorry there are no available rooms between the selected check in date and selected check out date! Please check the room availability again!");
                }
            }
            if (successfulCreate == true)
            {
                MessageBox.Show("Create Successful!");
                TimeSpan totalBookedDays = Convert.ToDateTime(dtpCreateBookingCheckOutDate.Text) - Convert.ToDateTime(dtpCreateBookingCheckInDate.Text);
                int iTotalBookedDays = Convert.ToInt32(totalBookedDays.TotalDays);
                for (int i = 0; i < iTotalBookedDays; i++)
                {
                    DBManager.UpdateRoomAvailabilityForCreateBooking(checkInDate.AddDays(i), cbxCreateBookingRoomType.Text);
                }
                DialogResult dialogResult = MessageBox.Show("Do you want to continue create bookings?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No)
                {
                    this.Close();
                }
                else
                {
                    ClearComboBoxes(this.Controls);
                    ClearTextBoxes(this.Controls);
                    ClearListView(this.Controls);
                    ClearComboBoxes(this.Controls);
                    ResetDateTimePicker(this.Controls);
                }
            }
            else
            {
                MessageBox.Show("Create Not Successful!");
            }
        }

        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

        private void ClearComboBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ComboBox cb = ctrl as ComboBox;
                if (cb != null)
                {
                    cb.Text = "";
                }

                else
                {
                    ClearComboBoxes(ctrl.Controls);
                }
            }
        }

        private void ClearListView(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ListView lv = ctrl as ListView;
                if (lv != null)
                {
                    lv.Items.Clear();
                }

                else
                {
                    ClearListView(ctrl.Controls);
                }
            }
        }

        private void ResetDateTimePicker(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                DateTimePicker dtp = ctrl as DateTimePicker;
                if (dtp != null)
                {
                    dtp.ResetText();
                }

                else
                {
                    ResetDateTimePicker(ctrl.Controls);
                }
            }
        }
    }
}
